package com.popcorn.aboard.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import com.popcorn.aboard.model.AboardModel;
import com.popcorn.aboard.model.AboardModel;

public class AboardDAO {
		Connection conn = null;
		PreparedStatement pstmt = null;
		DataSource ds = null;
		ResultSet rs = null;
		
		public AboardDAO(){
		try{
			Context init = new InitialContext();
			ds = (DataSource)init.lookup("java:comp/env/jdbc/Mysql");
			         
		}catch(Exception e){
			e.printStackTrace();
			}	  
		}
		
		//�?�� 개수 구하�?
		public int getListCount() {
			int x= 0;
			
			try{
				
				conn=ds.getConnection();
				System.out.println("getConnection");
				pstmt=conn.prepareStatement("select count(*) from aboard");
				rs = pstmt.executeQuery();
				
				if(rs.next()){
					x=rs.getInt(1);
				}
			}catch(Exception ex){
				System.out.println("getListCount ?�러: " + ex);			
			}finally{
				if(rs!=null) try{rs.close();}catch(SQLException ex){}
				if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
				if(conn!=null) try{conn.close();}catch(SQLException ex){}
			}
			return x;
		}
		
		//?�약 리스??보기
		public List modelList(int page,int limit){	
			String sql="";			
			List list = new ArrayList();			
//					int startrow=(page-1)*10+1; //?�기 ?�작??row번호.
//					int endrow=startrow+limit-1; //?�을 마�?�?row 번호.	
			try{
				try {
					conn = ds.getConnection();				
				} catch (Exception e) {
					System.out.println("connect Exception : "+e.toString());
				}
				 
				sql="select * from aboard order by SEQ_BBS desc";				
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				
				while(rs.next()){
					AboardModel model = new AboardModel();
					model.setSEQ_BBS(rs.getInt("SEQ_BBS"));
		//			model.setREG_ID(rs.getString("REG_ID"));
					model.setTITLE(rs.getString("TITLE"));
					model.setREG_DTM(rs.getString("REG_DTM"));
					model.setHIT_CNT(rs.getInt("HIT_CNT"));
					list.add(model);
				}				
				return list;
				
			}catch(Exception ex){
				System.out.println("보기 ?�패");
				ex.printStackTrace();
			}finally{
				if(rs!=null) try{rs.close();}catch(SQLException ex){}
				if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
				if(conn!=null) try{conn.close();}catch(SQLException ex){}
			}
			return null;
		}		
		
		public boolean modelInsert(AboardModel model){
			int result = 0;
			int num =0;
			String sql="";			
			
			try{
				try {
					conn = ds.getConnection();				
				} catch (Exception e) {
					System.out.println("connect Exception : "+e.toString());
				}
				pstmt=conn.prepareStatement("select max(SEQ_BBS) from aboard");
				rs = pstmt.executeQuery();
	
				if(rs.next())
					num =rs.getInt(1)+1;
				else
					num=1;
				// ?�짜 ?�간 겹치�??�게 ?�록 + �??�기
				sql="insert into aboard values(?,?,?,?,?,?)";
				
				pstmt = conn.prepareStatement(sql);
				pstmt.setInt(1, num);
				pstmt.setString(2, model.getTITLE());
				pstmt.setString(3, model.getCONTENT());
				pstmt.setInt(4, 0); //model.getHIT_CNT()
		//		pstmt.setString(5, model.getREG_ID());
				pstmt.setString(5, model.getREG_DTM());
				pstmt.setString(6, "0000-00-00");
				
				System.out.println("====================InsertDB====================");
				System.out.println("1 : "+num);
				System.out.println("2 : "+model.getTITLE());
				System.out.println("3 : "+model.getCONTENT());
				System.out.println("4 : "+0);
		//		System.out.println("5 : "+model.getREG_ID());
				System.out.println("5 : "+model.getREG_DTM());
				System.out.println("6 : "+"0000-00-00");
				result=pstmt.executeUpdate();
				
				if(result==0)return false;
				
				return true;
				
			}catch(Exception ex){
				System.out.println("?�록 ?�패");
				ex.printStackTrace();
			}finally{
				if(rs!=null) try{rs.close();}catch(SQLException ex){}
				if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
				if(conn!=null) try{conn.close();}catch(SQLException ex){}
			}
			return false;
		}
		
		public AboardModel modelDetail(int idx){
			String sql="";
			AboardModel model = new AboardModel();
			try{
				try {
					conn = ds.getConnection();				
				} catch (Exception e) {
					System.out.println("connect Exception : "+e.toString());
				}
				
				sql="select * from aboard where SEQ_BBS="+idx;				
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
			
				if(rs.next()){
					model.setSEQ_BBS(rs.getInt("SEQ_BBS"));
					model.setCONTENT(rs.getString("CONTENT"));
					model.setHIT_CNT(rs.getInt("HIT_CNT"));
					//model.setMOD_DTM(rs.getString("MOD_DTM"));
					//model.setMOD_ID(rs.getString("MOD_ID"));
					model.setREG_DTM(rs.getString("REG_DTM"));
					//model.setREG_ID(rs.getString("REG_ID"));
					model.setTITLE(rs.getString("TITLE"));
				}	
				
				sql="update aboard set HIT_CNT="+(model.getHIT_CNT()+1)+" where SEQ_BBS="+idx;
				pstmt = conn.prepareStatement(sql);
				pstmt.executeUpdate();
				
					return model;
					
			}catch(Exception ex){
				System.out.println("?�세보기 ?�패");
				ex.printStackTrace();
			}finally{
				if(rs!=null) try{rs.close();}catch(SQLException ex){}
				if(pstmt!=null) try{pstmt.close();}catch(SQLException ex){}
				if(conn!=null) try{conn.close();}catch(SQLException ex){}
			}
			return null;
		}
}